<template>
  <div class="student">
    <el-row :gutter="20" style="margin-bottom: 20px; text-align: center">
      <el-col :span="4">
        <el-button type="primary" @click="openDrawer">添加</el-button>
      </el-col>
      <el-col :span="8">
        <el-input v-model="search" placeholder="搜索姓名..." @input="handleSearch"></el-input>
      </el-col>
      <el-col :span="4">
        <el-select v-model="selected" placeholder="选择" @change="handleSelectChange">
          <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-col>
      <el-col :span="4">
        <el-button type="primary" @click="getAllStudentList(undefined)">取消</el-button>
      </el-col>
    </el-row>
    <el-table :data="paginatedUsers" style="width: 100%" stripe border>
      <el-table-column prop="name" label="姓名" align="center" width="150"></el-table-column>
      <el-table-column prop="age" label="年龄" align="center" width="100"></el-table-column>
      <el-table-column prop="sex" label="性别" align="center" :formatter="formatSex" width="100"></el-table-column>
      <el-table-column prop="iphone" label="电话" align="center"></el-table-column>
      <el-table-column prop="idcards" label="身份证号" align="center"></el-table-column>
      <el-table-column prop="createtime" label="入学日期" align="center" width="100"></el-table-column>
      <el-table-column prop="address" label="地址" align="center"></el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope">
          <div class="action-buttons">
            <el-button size="mini" icon="el-icon-edit" @click="handleEdit(scope.row)"></el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete" @click="handleDelete(scope.row)"></el-button>
            <el-button size="mini" type="info" icon="el-icon-view" @click="handleView(scope.row)"></el-button>
            <el-drawer title="我是标题" :visible.sync="drawer" :with-header="false">
              <div style="line-height: 8vh;">
                <h1>学生详情信息：</h1>
                <div>学生id:{{ statudyinfo.id }}</div>
                <div>学生年龄： {{ statudyinfo.age }}</div>
                <div>学生姓名： {{ statudyinfo.name }}</div>
                <div>学生手机号： {{ statudyinfo.iphone }}</div>
                <div>学生学历： {{ statudyinfo.xueli }}</div>
                <div>学生身份证号： {{ statudyinfo.idcards }}</div>
                <div>学生性别； {{ statudyinfo.sex }}</div>
                <div>学生地址： {{ statudyinfo.address }}</div>
                <div>备注： {{ statudyinfo.mark }}</div>
                <div>入学时间： {{ statudyinfo.createtime }}</div>
              </div>
            </el-drawer>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination background layout="total, sizes, prev, pager, next, jumper" :total="filteredUsers.length"
      :page-size="pageSize" @current-change="handlePageChange" @size-change="handleSizeChange"
      style="text-align: center; margin-top: 20px"></el-pagination>

    <!-- 抽屉组件 -->
    <el-drawer title="添加学生信息" :visible.sync="drawerVisible" size="50%" :destroy-on-close="true">
      <el-form ref="addForm" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="学历" prop="xueli">
          <el-input v-model="form.xueli"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="年纪" prop="age">
          <el-input v-model="form.age" type="number"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="iphone">
          <el-input v-model="form.iphone"></el-input>
        </el-form-item>
        <el-form-item label="身份证号码" prop="idcards">
          <el-input v-model="form.idcards"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择">
            <el-option label="男" :value="0"></el-option>
            <el-option label="女" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="mark">
          <el-input v-model="form.mark"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAddSubmit">提交</el-button>
          <el-button @click="closeDrawer">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import {
  getAllStudentList,
  addWxUser,
  delWxUser,
  getUsers,
  getUsersinfo
} from "../../api/login_http";

export default {
  data() {
    return {
      statudyinfo: [],
      drawer: false,
      users: [],
      search: "",
      selected: "",
      options: [],
      currentPage: 1,
      pageSize: 10,
      drawerVisible: false,
      buttonselect: 0,
      form: {
        xueli: "",
        name: "",
        age: null,
        iphone: "",
        idcards: "",
        sex: null,
        address: "",
        mark: "",
      },
      rules: {
        xueli: [{ required: false }],
        name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        age: [{ required: true, message: "请输入年龄", trigger: "blur" }],
        iphone: [{ required: false }],
        idcards: [
          { required: true, message: "请输入身份证号码", trigger: "blur" },
        ],
        sex: [{ required: true, message: "请选择性别", trigger: "change" }],
        address: [{ required: false }],
        mark: [{ required: false }],
      },
    };
  },
  computed: {
    filteredUsers() {
      return this.users.filter((user) => user.name.includes(this.search));
    },
    paginatedUsers() {
      const start = (this.currentPage - 1) * this.pageSize;
      const end = start + this.pageSize;
      return this.filteredUsers.slice(start, end);
    },
  },
  created() {
    this.getAllStudentList();
    this.getUsers();
  },
  methods: {
    getAllStudentList(value) {
      if (value) {
        const obj = {
          page: this.currentPage,
          pagelimit: 100,
          webid: value,
        };
        getAllStudentList(obj).then((res) => {
          if (res.msg == "学生列表空") {
            this.users = [];
          } else {
            this.users = res.data.data;
          }
        });
      } else {
        this.selected = null;
        const obj = {
          page: this.currentPage,
          pagelimit: 1000,
        };
        getAllStudentList(obj).then((res) => {
          this.users = res.data.data;
          console.log(res.data.data);
        });
      }
    },
    getUsers() {
      getUsers().then((res) => {
        this.options = res.data.map((user) => ({
          value: user.webid,
          label: user.webname,
        }));
      });
    },
    handleSelectChange(value) {
      this.getAllStudentList(value);
    },
    openDrawer() {
      this.buttonselect = 0;
      this.drawerVisible = true;
    },
    closeDrawer() {
      this.drawerVisible = false;
      this.$refs.addForm.resetFields();
    },
    handleAddSubmit() {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          if (this.buttonselect == 0) {
            const obj = { ...this.form, webid: 1 };
            addWxUser(obj).then((res) => {
              console.log(res);
              this.drawerVisible = false;
              this.$refs.addForm.resetFields();
              this.getAllStudentList();
            });
          } else {
            const obj = { ...this.form, id: this.buttonselect };
            addWxUser(obj).then((res) => {
              console.log(res);
              this.drawerVisible = false;
              this.$refs.addForm.resetFields();
              this.getAllStudentList();
            });
          }
        } else {
          console.log("表单验证失败");
          return false;
        }
      });
    },
    handleSearch() { },
    handlePageChange(page) {
      this.currentPage = page;
      console.log("当前页：" + page);
    },
    handleSizeChange(size) {
      this.pageSize = size;
      this.currentPage = 1; // 当切换每页条数时，回到第一页
    },
    handleEdit(data) {
      console.log(data.id);
      this.buttonselect = data.id;
      this.form = { ...data };
      this.drawerVisible = true;
    },
    //删除事件
    handleDelete(row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        //框架自带参数
        const obj = {
          id: row.id,
        };
        delWxUser(obj).then((res) => {
          console.log(res);
          this.getAllStudentList();
        });
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    handleView(row) {
      // 查看详细的逻辑
      this.drawer = true

      const obj = {
        id: row.id,
      };
      getUsersinfo(obj).then((res) => {
        console.log(res);
        this.statudyinfo = res.data;
      });
    },
    formatSex(row, column, value) {
      return value === 0 ? "男" : "女";
    },
  },
};
</script>

<style scoped>
.student {
  padding: 5vh;
  box-sizing: border-box;
  background-color: aliceblue
}

.el-row {
  margin-bottom: 20px;
  text-align: center;
}

.el-col {
  padding-right: 10px;
}

.el-header,
.el-footer {
  background-color: #b3c0d1;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-aside {
  background-color: #d3dce6;
  color: #333;
  text-align: center;
  line-height: 200px;
}

.el-main {
  background-color: #e9eef3;
  color: #333;
}

body>.el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}

.no-underline {
  text-decoration: none;
}

.action-buttons {
  display: flex;
  justify-content: center;
  gap: 10px;
}
</style>
